package com.zhipu.balance.ui.vm.home.calculate.rice

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordRice
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils


/**
 * 出米率
 */
class RiceYieldViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 稻谷实体
     */
    var mRecordRice : RecordRice? = null

    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null

    /**
     * 总稻谷量
     */
    var totalPaddyQuality = ObservableField("")
    /**
     * 出米量
     */
    var riceQuality = ObservableField("")
    /**
     * 出米率
     */
    var yieldRate = ObservableField("")





    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordRice?.yieldRateRecorder)){
            //总稻谷量
            totalPaddyQuality.set(mRecordRice?.totalPaddyQuality.toString())
            //出米量
            riceQuality.set(mRecordRice?.riceQuality.toString())
            //出米率
            yieldRate.set(mRecordRice?.yieldRate.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //总稻谷量
        totalPaddyQuality.set("")
        //出米量
        riceQuality.set("")
        //出米率
        yieldRate.set("")
    }







    /**
     * 总稻谷量
     */
    fun BulkQuality(){
        totalPaddyQuality.set(weighing)
        mRecordRice?.totalPaddyQuality = totalPaddyQuality?.get()?.toDouble()
    }

    /**
     * 出米量
     */
    fun YellowGrainRiceQuality(){
        riceQuality.set(weighing)
        mRecordRice?.riceQuality = riceQuality.get()?.toDouble()
    }





    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算整精米率
         */
        var mYieldRate = CommonUtils.formatDouble(mRecordRice!!.riceQuality / mRecordRice!!.totalPaddyQuality * 100)
        mRecordRice!!.yieldRate = mYieldRate
        yieldRate.set(mYieldRate.toString())

        /**
         * 出米率记录员
         */
        mRecordRice!!.yieldRateRecorder = mPersonnel!!.name


        if(mRecordRice?.totalPaddyQuality == 0.00){
            totalPaddyQuality.set("0.00")
        }
        if(mRecordRice?.riceQuality == 0.00){
            riceQuality.set("0.00")
        }
    }
}